package de.dvdverleih.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.dvdverleih.dao.DaoException;
import de.dvdverleih.dao.KundenDao;
import de.dvdverleih.model.Dvd;
import de.dvdverleih.model.Kunden;

/**
 * @author Landsberger, Neitzsch, Baier
 */

@Controller
public class KundenController {
	private static Logger log = Logger.getLogger(KundenController.class);
	private KundenDao kundenDao = null;

	public void setKundenDao(KundenDao kundenDao) {
		this.kundenDao = kundenDao;
	}

	/**
	 * 
	 * <p>Creates new {@link Kunden}.</p>
	 * <p>Expected HTTP Get and request '/KundenAnlegen.html'.</p>
	 * @param id primary key of the customer
	 * 
	 */
	@RequestMapping(value = "/KundeAnlegen.html", method = RequestMethod.GET)
	public ModelAndView edit1(@RequestParam(required = false) Integer id) {
		log.debug("KundeAnlegen.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Kunden());
		} else {
			mv.addObject(kundenDao.findById(id));
		}
		mv.setViewName("kunde-anlegen");
		return mv;
	}
	
	/**
	 * 
	 * <p>Edits {@link Kunden}.</p>
	 * <p>Expected HTTP Get and request '/KundenBearbeiten.html'.</p>
	 * @param id primary key of the customer
	 * 
	 */
	@RequestMapping(value = "/KundeBearbeiten.html", method = RequestMethod.GET)
	public ModelAndView edit2(@RequestParam(required = true) Integer id) {
		log.debug("KundeBearbeiten.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Kunden());
		} else {
			mv.addObject(kundenDao.findById(id));
		}
		mv.setViewName("kunde-bearbeiten");
		return mv;
	}

	/**
	 * 
	 * <p>Saves {@link Kunden} in the database.</p>
	 * <p>Expected HTTP POST and request '/SpeichereDvd.html'.</p>
	 * @return a redirect to page '/index.html' - homepage for customers
	 *
	 */
	@RequestMapping(value = "/SpeichereKunden.html", method = RequestMethod.POST)
	public String save1(Kunden kunden, Model model) {
		kundenDao.save(kunden);
		return "redirect:index.html";
	}
	
	/**
	 * 
	 * <p>Saves {@link Kunden} in the database, if changes have been made.</p>
	 * <p>Expected HTTP POST and request '/SpeichereKundenAenderung.html'.</p>
	 * @return a redirect to page '/AlleKunden.html' for editing customers
	 * 
	 */
	@RequestMapping(value = "/SpeichereKundenAenderung.html", method = RequestMethod.POST)
	public String save2(Kunden kunden, Model model) {
		kundenDao.save(kunden);
		return "redirect:AlleKunden.html";
	}

	/**
	 * 
	 * <p>Deletes {@link Kunden} from database.</p>
	 * <p>Expected HTTP GET and request '/LoescheKunde.html'.</p>
	 * @return a redirect to page '/AlleKunden.html' for editing customers
	 * 
	 */
	@RequestMapping(value = "/LoescheKunde.html", method = RequestMethod.GET)
	public ModelAndView delete(Kunden kunden) {
		try{
		kundenDao.delete(kunden);
		return new ModelAndView ("redirect:AlleKunden.html");
		}catch(DaoException e){
			ModelAndView mv = new ModelAndView("exception-error");
			mv.addObject("message", e.getMessage());
			return mv;
		}
	}

	/**
	 * 
	 * <p>Page of the admin for editing {@link Kunden}.</p>
	 * <p>Finds all customers from the database.</p>
	 * <p>Expected HTTP GET and request '/AlleKunden.html'.</p>
	 * 
	 */
	@RequestMapping(value = "/AlleKunden.html", method = RequestMethod.GET)
	public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Kunden");
		mv.addObject("message", "Alle Kunden");
		mv.addObject("kunden", kundenDao.findAll());
		mv.setViewName("list-kunden");
		return mv;
	}
		
		/**
		 * 
		 * <p>Login page for the admin to edit {@link Dvd}s and {@link Kunden}.</p>
		 * <p>Expected HTTP GET and request '/Login.html'.</p>
		 *  
		 */
		@RequestMapping(value = "/Login.html", method = RequestMethod.GET)
		public ModelAndView login() {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("LoginPage");
			return mv;
		}
			
			/**
			 * 
			 * <p>The admin menu itself for editing {@link Dvd}s and {@link Kunden}.</p>
			 * <p>Expected HTTP GET and request '/Adminmenu.html'.</p> 
			 * 
			 */
			@RequestMapping(value = "/Adminmenu.html", method = RequestMethod.GET)
			public ModelAndView Admin() {
				ModelAndView mv = new ModelAndView();
				mv.setViewName("Adminmenu");
				return mv;
	}
	

	
}
